package com.plasticcode.log.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@GenericGenerator(
        name = "RunGenerator",
        strategy = "hilo", 
        parameters = { @Parameter(name="max_lo", value="1") })

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Runner implements Serializable {
    private Long id;
    private String name;
    private int age;
    private Address address;

    private List<Run> runs = new ArrayList<Run>();

    @Id @GeneratedValue(strategy=GenerationType.TABLE, generator="RunGenerator")
    public Long getId() {
        return id;
    }
    
    private void setId(Long id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    
    public Address getAddress() {
        return address;
    }

    @Embedded
    public void setAddress(Address address) {
        this.address = address;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "runner") // mapped by indicates this is not the owner of the relationship
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public List<Run> getRuns() {
        return runs;
    }
    
    public void addRun(Run run) {
        runs.add(run);
        run.setRunner(this);
    }
    
    public void setRuns(List<Run> runs) {
        this.runs = runs;
    }
}
